package com.lynn.salary.web.action;

import com.lynn.salary.entity.User_;
import com.lynn.salary.pojo.LoginUser;
import com.lynn.salary.service.IUserService;
import com.lynn.salary.util.ObjectFactory;

public class LoginAction extends BaseAction {

	private String username;
	private String password;
	private IUserService userService = (IUserService) ObjectFactory.getBean("userService");
	private String flag;
	private User_ user;
	private String errorMsg;

	public String execute() {
		System.out.println("username:" + username);
		System.out.println("password:" + password);
		if (session.get("loginUser") != null)
			return SUCCESS;
		if (username == null || password == null || username.trim().equals("") || password.trim().equals("")) {
			errorMsg = "*请正确填写用户名或密码";
			return "relogin";
		}

		User_ e = null;
		try {
			e = userService.checkLoginUser(username, password);
			if (e == null || !e.getType().trim().equals(User_.ADMIN)) {
				errorMsg = "*请正确填写用户名或密码";
				return "relogin";
			}
			if(e.getTag()!= null && e.getTag().contains(User_.FORBIDDEN)) {
				errorMsg = "*您的帐户已被管理员禁用";
				return "relogin";
			}
			LoginUser loginUser = new LoginUser();
			loginUser.setLoginUser(e);
			session.put("loginUser", loginUser);
		} catch (Exception e1) {
			e1.printStackTrace();
			return "error";
		}
		return SUCCESS;
	}

	public String frontEndLogin() throws Exception {
		
		boolean isLogin = isFrontLogin();
		if(isLogin) {
			return "alreadyLogin";
		}
		
		if (flag == null || flag.trim().equals("")) {// 显示注册页面
			return "showLoginPage";
		}

		String id = user.getId();
		String password = user.getPassword();
		System.out.println("用户名:" + id + "\n密码:" + password);

		User_ u = userService.checkLoginUser(id, password);
		if (u == null) {
			errorMsg = "*请正确填写用户名或密码";
			return "frontEndRelogin";
		}
		if(u.getTag()!= null && u.getTag().contains(User_.FORBIDDEN)) {
			errorMsg = "*您的帐户已被管理员禁用";
			return "frontEndRelogin";
		}

		LoginUser loginUser = new LoginUser();
		loginUser.setLoginUser(u);
		session.put("frontEndLoginUser", loginUser);

		return SUCCESS;
	}
	
	public String loginStatus() throws Exception {
		if(isFrontLogin()) {
			return "alreadyLoginStatus";
		} else {
			return "notLoginStatus";
		}
	}
	
	public String showInfo() throws Exception {
		if(isFrontLogin()) {
			return "alreadyLoginPage";
		} else {
			return "notLoginPage";
		}
	}
	
	private boolean isFrontLogin() {
		return frontEndLoginUser!=null && frontEndLoginUser.getLoginUser() != null;
	}

	public String admin() {
		return SUCCESS;
	}

	public String logout() {
		session.clear();
		return "relogin";
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getErrorMsg() {
		return errorMsg;
	}

	public void setErrorMsg(String errorMsg) {
		this.errorMsg = errorMsg;
	}

	public IUserService getUserService() {
		return userService;
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}

	public User_ getUser() {
		return user;
	}

	public void setUser(User_ user) {
		this.user = user;
	}

}
